﻿<script type="text/javascript">
    boolSearch = true;
    var booContain = 0;//20151126接收是否为"装备部汽配仓库"
    function initDetailGrid(Contain) {
        $('#btnDelivery').linkbutton('enable');

        $.InitControlDateTime("#DeliveryDateTime");
        $("#DeliveryRemark").textbox("clear");
        $("#DeliveryStorage").combobox('setText', '');//20151126增加刚加载成功后，“物资去向”默认为空
        booContain = Contain;//20151126
        if (Contain == 0) {
            $('#lblDeliveryPerson').html("领取人：");
            var urlinfo = '@Url.Content("~/IM/AllDictionary/GetUsers")';
            $('#DeliveryPerson').combobox({
                prompt: '--请选择--',
                url: urlinfo,
                valueField: 'UserID',
                textField: 'Name',
                method: 'POST',
                filter: function (q, row) {
                    var opts = $('#DeliveryPerson').combobox('options');
                    return row[opts.textField].indexOf(q) > -1;
                },
                onLoadSuccess: function (rec) {
                    $("#DeliveryPerson").combobox('setText', '');//20151109增加刚加载成功后，领取人默认为空
                },
                onSelect: function (rec) {
                    var urlstr1 = '@Url.Content("~/IM/AllDictionary/GetStogageByUser?userId=")' + rec.UserID;
                    $.BindCombox("#DeliveryStorage", urlstr1);
                }
            });
        }
        else {
            $('#lblDeliveryPerson').html("车牌号码：");
            var urlinfo = '@Url.Content("~/IM/AllDictionary/GetAmbulances")';
            $('#DeliveryPerson').combobox({
                prompt: '--请选择--',
                url: urlinfo,
                valueField: 'AmbulanceCode',
                textField: 'PlateNumber',
                method: 'POST',
                filter: function (q, row) {
                    var opts = $('#DeliveryPerson').combobox('options');
                    return row[opts.textField].indexOf(q) > -1;
                },
                onLoadSuccess: function (rec) {
                    $("#DeliveryPerson").combobox('setText', '');//20151126--增加刚加载成功后，领取人默认为空
                },
                onSelect: function (rec) {
                    $('#DeliveryStorage').combobox('setText', '使用库');//20151126--装备部汽配仓库只能出到使用库
                },
                onChange: function (rec) {
                    $("#DeliveryStorage").combobox('setText', '');//20151126--增加改变输入值，“物资去向”默认为空
                }
            });
        }

        $("#gridDeliveryDetailList").datagrid({
            scrollbarSize: 18,
            rownumbers: true,
            singleSelect: true,
            width: 'auto',
            height: 245,
            fitColumns: true,
            striped: true,
            singleSelect: true,
            loadMsg: '数据加载中请稍后……',
            pagination: false,//启用分页，默认每页10行
            columns: [
                [
                   { title: '物资名称', field: 'MaterialName', width: '10%', sortable: false },
                   { title: '批次号', field: 'RealBatchNo', width: '10%', sortable: false, formatter: $.formatRealBatchNo },
                   { title: '有效期', field: 'ValidityDate', width: '10%', sortable: false, formatter: $.formatValidityDate },
                   { title: '当前结余', field: 'Surplus', width: '5%', sortable: false },
                   { title: '仓库', field: 'StorageName', width: '10%', sortable: false },
                   { title: '出库时间', field: 'DeliveryDetailDateTime', width: '10%', sortable: false },
                   {
                       field: 'DeliveryDetailCounts', title: '出库数量', width: '10%',
                       formatter: function (value, rec, rowIndex) {
                           return "<input id=\"DeliveryCounts" + rowIndex + "\" name = \"DeliveryCounts\"  style=\"width:80px\" value='1'/>";
                       }
                   },
                   {
                       field: 'DeliveryDetailRemark', title: '备注', width: '30%',
                       formatter: function (value, rec, rowIndex) {
                           return "  <input id=\"DeliveryDetailRemark" + rowIndex + "\" name = \"DeliveryDetailRemarks\"  style=\"width:98%\"/>";
                       }
                   },
                   { title: 'StorageCode', field: 'StorageCode', sortable: false, hidden: true },
                   { title: 'ID', field: 'ID', sortable: false, hidden: true },
                   { title: 'MaterialID', field: 'MaterialID', sortable: false, hidden: true }
                ]
            ],
            toolbar: [
                    {
                        id: 'btnDetailDelete',
                        text: '删除出库信息',
                        iconCls: 'icon-custom-Delete',
                        handler: function () {
                            var selected = $('#gridDeliveryDetailList').datagrid('getSelected');
                            if (selected) {
                                deleteDeliveryDetailJsondate(selected.ID);
                            } else {
                                $.messager.alert('提示', '请选择数据行', 'info', function () {
                                });
                            }
                        }
                    }
            ]
        });
        $("#gridDeliveryDetailList").datagrid("loadData", Materials);
        setControl();
    }
    function submitDeliveryDetailForm() {
        var isValid = $("#DeliveryDetailList").form("validate");//20151103增加必填项校验
        if (isValid) {

            $('#btnDelivery').linkbutton('disable');
            var jsData = new Array();
            var i = 0;
            var IsSameStorage = 0;//20151103相同的仓库
            var IsSameMaterial = 0;//20151130判断是否相同物资，相同批次等
            var DeliveryStorage = booContain==1? "-1" : $("#DeliveryStorage").combotree("getValue");//20151103物资去向
            var rows = $('#gridDeliveryDetailList').datagrid('getRows');
            $("input[name='DeliveryCounts']").each(function () {
                var obj = new Object();
                obj.ID = rows[i].ID;
                obj.EntryStorageCode = rows[i].StorageCode;
                //20151103增加如果物资去向与要出库所在库一样的就不允许出库
                if (DeliveryStorage == rows[i].StorageCode) {
                    IsSameStorage = 1;
                }
                obj.MaterialID = rows[i].MaterialID;
                obj.BatchNo = rows[i].BatchNo;
                obj.RealBatchNo = rows[i].RealBatchNo;
                obj.TargetStorageCode = booContain == 1 ? -1 : $("#DeliveryStorage").combotree("getValue");//如果为汽配仓库目的仓库编码为-1
                obj.Remark = "";
                obj.DeliveryCounts = $(this).val();
                //20151130--增加判断要出库的物资，如果是相同物资，并且批次号相同，只是所在仓库不同，不允许出库；
                $.each(jsData, function (i, info) {
                    if (info.MaterialID == obj.MaterialID && info.BatchNo == obj.BatchNo && info.RealBatchNo == obj.RealBatchNo) {
                        IsSameMaterial = 1;
                    }
                });
                if (IsSameMaterial == 0) {
                    jsData.push(obj);
                }

                i++;
            });
            if (IsSameMaterial == 1) {
                $.showMsg("同物资并且同批次号不允许同时出库！请分批次出库！", "提示");//20151103
                $('#btnDelivery').linkbutton('enable');
                return;
            }
            if (IsSameStorage == 1) {
                $.showMsg("物资去向与要出库的仓库相同不允许出库！", "提示");//20151103
                $('#btnDelivery').linkbutton('enable');
            }
            else {
                var j = 0;
                $("input[name='DeliveryDetailRemarks']").each(function () {
                    jsData[j].Remark = $(this).val();
                    j++;
                });

                $.ajax({
                    type: 'POST',
                    url: '@Url.Content("~/I_Delivery/SaveDeliveryDetail")',
                    data:
                        {
                            ConsigneeID: booContain==1? "-1": $('#DeliveryPerson').combobox("getValue"),
                            DeliveryDate: $("#DeliveryDateTime").datebox('getValue'),
                            ReceivingStoreID: booContain == 1 ? -1 : $("#DeliveryStorage").combotree("getValue"),
                            Remark: $("#DeliveryRemark").val(),
                            sDeliveryDetailInfo: JSON.stringify(jsData),
                            AmbulanceCode: booContain == 1 ? $('#DeliveryPerson').combobox("getValue") : ""

                        },
                    dataType: "json",
                    success: function (jsData) {
                        $.handleJsonResult(jsData, function () {
                            Materials.length = 0;
                            $("#gridSurplusList").datagrid("reload");
                            $('#DeliveryDetailList').dialog("close");
                        }, function () {
                            $('#btnDelivery').linkbutton('enable');
                        });
                    }
                });
            }

        }
        else {
            $.showMsg("请选择红色框的内容！", "提示");//20151103
        }
    }
    function deleteDeliveryDetailJsondate(ecode) {
        var obj;
        for (var i = 0; i < Materials.length; i++) {
            if (ecode == Materials[i].ID) {
                obj = Materials[i];
                break;
            }
        }
        Materials.remove(obj);
        $("#gridDeliveryDetailList").datagrid("loadData", Materials);
        changeDeliveryViewNum();
        setDeliveryViewNumCheck();
        setControl();
    }
    function setControl() {
        for (var i = 0; i < Materials.length; i++) {
            $.initDataGridTextBoxNum("#DeliveryCounts" + i, Materials[i].Surplus, true);
            $('#DeliveryDetailRemark' + i).textbox({
                multiline: true,
                width: 200,
                height: 30
            });
        }
    }
</script>
<div id="DeliveryDetailList" class="easyui-dialog" title="出库详细清单" data-options="modal:true,collapsible:false,minimizable:false,maximizable:false,closed:true,resizable:false,iconCls:'icon-custom-cDelivery'" style="width:1000px;height:400px;padding:0px;">
    <div data-options="region:'north'" style="overflow: hidden; padding: 10px;width:900px;">
        <table>
            <tr>
                <td width="100px" style="vertical-align:text-top;text-align:right">出库时间：</td>
                <td width="200px" style="vertical-align:text-top">
                    <input id="DeliveryDateTime" class="easyui-datetimebox" style="width:150px" data-options="required:true" />
                </td>
                <td width="100px" style="vertical-align: text-top; text-align: right"><label id="lblDeliveryPerson"></label></td>
                <td width="200px"><input id="DeliveryPerson" class="easyui-combobox" data-options="required:true" /></td>
                <td width="100px" style="vertical-align: text-top; text-align: right">物资去向：</td>
                <td width="200px"><input id="DeliveryStorage" class="easyui-combotree" data-options="required:true" /></td>
            </tr>
            <tr>
                <td width="100px" style="vertical-align:text-top;text-align:right">备注：</td>
                <td colspan="4">
                    <input id="DeliveryRemark" class="easyui-textbox" data-options="multiline:true" style="width:500px;height:50px">
                </td>
                <td style="text-align:right; vertical-align:bottom">
                    <a href="javascript:void(0)" id="btnDelivery" class="easyui-linkbutton" data-options="iconCls:'icon-custom-cDelivery'" onclick="submitDeliveryDetailForm()">&nbsp;&nbsp;确认出库&nbsp;&nbsp;</a>
                    &nbsp;&nbsp;
                </td>
            </tr>
        </table>
    </div>
    <div data-options="region:'center'" style="background: #fafafa; overflow: hidden">
        <table id="gridDeliveryDetailList"></table>
    </div>
</div>